package com.ruoyi.bom.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.bom.domain.TqlBom;
import com.ruoyi.bom.domain.TqlDishBom;
import com.ruoyi.bom.domain.TqlDishBomLog;
import com.ruoyi.bom.dto.DishBomLockDTO;
import com.ruoyi.bom.dto.DishBomVerifierDTO;
import com.ruoyi.bom.service.ITqlBomService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.bom.service.ITqlDishBomService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 菜品BOM关系Controller
 * 
 * @author ruoyi
 * @date 2023-12-31
 */
@RestController
@Api(tags = "菜品BOM管理")
@RequestMapping("/dishbom")
public class TqlDishBomController extends BaseController {

    @Autowired
    private ITqlDishBomService tqlDishBomService;

    @RequiresPermissions("dish:bom:all")
    @GetMapping("/page")
    @ApiOperation("菜品分页")
    public TableDataInfo page(@Parameter(hidden = true) @RequestParam Map<String, String> params) {
        //获取当前登录人shopId

        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            String deptCode = SecurityUtils.getLoginUser().getSysUser().getDeptCode();
            params.put("deptCode",deptCode);
        }

        startPage();
        List<TqlDishBom> list = tqlDishBomService.selectTqlDishBomList(params);
        return getDataTable(list);
    }

    @RequiresPermissions("dish:bom:all")
    @GetMapping("/bompage")
    @ApiOperation("半成品物料分页")
    public TableDataInfo bompage(@Parameter(hidden = true) @RequestParam Map<String, String> params) {
        startPage();
        List<TqlDishBom> list = tqlDishBomService.selectTqlBomList(params);
        return getDataTable(list);
    }

    @RequiresPermissions("dish:bom:pageLog")
    @ApiOperation("操作日志分页")
    @Parameters({
        @Parameter(name = "type", description = "1：修改 2：锁定 3：审批"),
    })
    @GetMapping("/page/log")
    public TableDataInfo pageLog(@RequestParam("id") String id, @RequestParam("type") Integer type) {
        startPage();
        List<TqlDishBomLog> list = tqlDishBomService.selectLogList(id, type);
        return getDataTable(list);
    }

    @RequiresPermissions("dish:bom:all")
    @Log(title = "导出菜品", businessType = BusinessType.EXPORT)
    @ApiOperation("导出菜品")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @Parameter(hidden = true) @RequestParam Map<String, String> params) {
        List<TqlDishBom> list = tqlDishBomService.selectTqlDishBomList(params);
        ExcelUtil<TqlDishBom> util = new ExcelUtil<>(TqlDishBom.class);
        util.exportExcel(response, list, "菜品BOM关系数据");
    }

    /**
     * 新增菜品BOM关系
     */
    @RequiresPermissions("dish:bom:add")
    @Log(title = "新增菜品Bom", businessType = BusinessType.INSERT)
    @ApiOperation("新增菜品Bom")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TqlDishBom tqlDishBom) {
        //检查数据库中有没有当前菜品
        if(tqlDishBom.getType().equals(0) && null==tqlDishBom.getFoodCode()) {

            Random random = new Random();
            int randomNumber = random.nextInt(9000) + 1000;
            tqlDishBom.setFoodCode("9999"+randomNumber);
        }
        else {
            int count = tqlDishBomService.checkFoodExistDish(null, tqlDishBom.getFoodCode());
            if (count > 0) return error("该菜品已经存在");
        }
        //获取用户信息
        String createdBy = SecurityUtils.getUsername();
        tqlDishBom.setCreatedBy(createdBy);
        tqlDishBom.setCreatedTime(new Date());
        tqlDishBomService.add(tqlDishBom);
        return success();
    }

    @ApiOperation("获取详细信息")
    @RequiresPermissions("dish:bom:all")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        TqlDishBom entity = tqlDishBomService.getById(id);
        return success(entity);
    }

    @RequiresPermissions("dish:bom:update")
    @Log(title = "修改菜品", businessType = BusinessType.UPDATE)
    @ApiOperation("修改菜品")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody TqlDishBom tqlDishBom) {
        int count = tqlDishBomService.checkFoodExistDish(tqlDishBom.getId(), tqlDishBom.getFoodCode());
        if(count > 0) return error("该菜品已经存在");
        String createdBy = SecurityUtils.getUsername();
        tqlDishBom.setUpdatedBy(createdBy);
        tqlDishBom.setUpdatedTime(new Date());
        tqlDishBomService.updateById(tqlDishBom);
        return success();
    }

    @RequiresPermissions("bom:bom:id")
    @Log(title = "审核菜品", businessType = BusinessType.UPDATE)
    @ApiOperation("审核菜品")
    @PutMapping("/verifier")
    public AjaxResult verifier(@RequestBody DishBomVerifierDTO dto) {
        return toAjax(tqlDishBomService.verifier(dto));
    }

    @RequiresPermissions("dish:bom:lockBatch")
    @Log(title = "锁定菜品", businessType = BusinessType.UPDATE)
    @ApiOperation("锁定菜品 0：未锁定 1：锁定")
    @PutMapping("/lockBatch")
    public AjaxResult lockBatch(@RequestBody List<DishBomLockDTO> ids) {
        return toAjax(tqlDishBomService.lockBatch(ids));
    }

    @ApiOperation("删除菜品")
    @RequiresPermissions("dish:bom:remove")
    @Log(title = "删除菜品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        tqlDishBomService.removeById(id);
        return success();
    }
}
